using System;
using System.Data;
using HRM.Common;
namespace HRM.BusinessLogic
{
    public class DepartmentInfo : Standard_Info
    {

        public DepartmentInfo()
        {
        }


        public int Department_ID { get; set; }
        public string Department_Code { get; set; }
        public string Department_Name { get; set; }
        public string Department_Address { get; set; }
        public DateTime Department_CreateDate { get; set; }
        public DateTime Department_LastUpdate { get; set; }
        public string Department_LastUser { get; set; }
        public int Department_BranchID { get; set; }
        public Boolean ProfitAndLossFlag { get; set; }
        public string Department_Description { get; set; }
        public int Department_MasterID { get; set; }
        public Boolean Department_IsHRM { get; set; }

    }

    public class Department_Controller : Standard_Controller
    {
        #region "Contructor"
        private static Department_Controller _instance;
        protected Department_Controller() { }
        protected new void Dispose()
        {
            _instance = null;
        }
        private static object _syncLock = new object();
        public static Department_Controller Instance()
        {
            if (_instance == null)
            {
                lock (_syncLock)
                {
                    if (_instance == null)
                        _instance = new Department_Controller();
                }
            }
            return _instance;
        }
        public static Department_Controller Instance(String transName)
        {
            if (!String.IsNullOrEmpty(transName))
            {
                Department_Controller _instanceTrans = new Department_Controller();
                _instanceTrans.Trans_Name = transName;
                return _instanceTrans;
            }
            else
                return Instance();
        }
        #endregion

        public const String cs_Table_Name = "HRM_Department";

        public int Department_Using(int DepartmentID)
        {
            return Globals.Object_GetValue<int>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Department_Using",false), DepartmentID));
        }

        public DataSet Department_GetAll()
        {
            return DataConnect.ExecuteDataset(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Department_GetAll", false));
        }

        public DataSet Department_GetList(out int ItemCount, int branchID, int currentPage, int pageSize)
        {
            ItemCount = 0;
            DataSet ds = DataConnect.ExecuteDataset(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Department_GetList", false), branchID,currentPage, pageSize);
            if (ds != null && ds.Tables.Count > 0)
                ItemCount = Globals.Object_GetValue<int>(ds.Tables[1].Rows[0][0]);
            return ds;
        }
        public DataSet Department_GetByMasterID(int branchID, int MasterID)
        {
            DataSet ds = DataConnect.ExecuteDataset(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Department_GetByMasterID", false),branchID, MasterID);
            return ds;
        }
        public DepartmentInfo Department_GetInfo(int DepartmentID)
        {
            return CBO.FillObject<DepartmentInfo>(DataConnect.ExecuteRow(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Department_GetInfo", false), DepartmentID));
        }

        public int Department_Insert(DepartmentInfo eInfo)
        {
            //
            eInfo.Department_IsHRM = true;
            return Globals.Object_GetValue<int>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Department_Insert", false),
                eInfo.Department_BranchID, 
                eInfo.Department_MasterID,
                eInfo.Department_Code,
                eInfo.Department_Name,
                eInfo.Department_Description,
                eInfo.Department_IsHRM));
        }

        public bool Department_Update(DepartmentInfo eInfo)
        {
            //
            eInfo.Department_IsHRM = true;
            return Globals.Object_GetValue<bool>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Department_Update", false), 
                eInfo.Department_ID,
                eInfo.Department_BranchID,
                eInfo.Department_MasterID,
                eInfo.Department_Code,
                eInfo.Department_Name,
                eInfo.Department_Description,
                eInfo.Department_IsHRM));
        }

        public bool Department_Delete(int DepartmentID)
        {
            return Globals.Object_GetValue<bool>(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, Procedure_Name(cs_Table_Name, "HRM_Department_Delete", false), DepartmentID));
        }


        public void InsertTable(DataTable dtData)
        {
            using (var dt = dtData.GetChanges(DataRowState.Deleted))
            {
                if(dt!=null)
                {
                    var dataTable = dt.GetChanges(DataRowState.Deleted);
                    if (dataTable != null)
                        foreach (DataRow dr in dataTable.Rows)
                        {
                            Department_Delete(Globals.DB_GetValue<int>(dr["Department_ID", DataRowVersion.Original]));
                        }
                }
            }
            using (var dt = dtData.GetChanges(DataRowState.Added))
            {
                if (dt != null)
                {
                    var dataTable = dt.GetChanges(DataRowState.Added);
                    if (dataTable != null)
                        foreach (DataRow dr in dataTable.Rows)
                        {
                            var objInfo = new DepartmentInfo();
                            Globals.DataRow_FillObject(dr, objInfo);
                            Department_Insert(objInfo);
                        }
                }
            }
            using (var dt = dtData.GetChanges(DataRowState.Modified))
            {
                if (dt != null)
                {
                    var dataTable = dt.GetChanges(DataRowState.Modified);
                    if (dataTable != null)
                        foreach (DataRow dr in dataTable.Rows)
                        {
                            var objInfo = new DepartmentInfo();
                            Globals.DataRow_FillObject(dr, objInfo);
                            Department_Update(objInfo);
                        }
                }
            }

            //
            dtData.AcceptChanges();
            //
           
        }

    }
}
